extends Control

var selected: int = 0

@onready var enemy = get_tree().get_first_node_in_group("Enemy")
@onready var player = get_tree().get_first_node_in_group("Player")
@onready var battle_ground = get_tree().get_first_node_in_group("BattleGround")
#父场景用到
@onready var action: Label = %Action
var battle_victory_defeat = preload("res://scene/battle_victory_defeat/battle_victory_defeat.tscn")
var new_battle_victory_defeat: CanvasLayer

func _ready() -> void:
	%MenuFight.hide()
	%MenuSwitch.hide()
	%Menu.show()
	new_battle_victory_defeat = battle_victory_defeat.instantiate()
	add_child(new_battle_victory_defeat)
	%EnemyHPBar.value = MySpriteData.SelectedDokiMons[selected]["Health"]
	%EnemyInfo.text = str(MySpriteData.SelectedDokiMons[selected]["Name"]) +" LV." + str(MySpriteData.SelectedDokiMons[selected]["Level"])
	%EnemyHPTxt.text = "HP: " + str(MySpriteData.SelectedDokiMons[selected]["Health"]) 
	#设置技能名字
	for i in MySpriteData.SelectedDokiMons[selected]["Attacks"]:
		var path = "MenuFight/GridContainer/Attack" + str(((i)+1))
		get_node(path).text = MySpriteData.SelectedDokiMons[selected]["Attacks"][i]["Name"]

	switchUpdate()

func _on_fight_pressed() -> void:
	%Menu.hide()
	%MenuSwitch.hide()
	%MenuFight.show()

func _on_switch_pressed() -> void:
	%Menu.hide()
	%MenuSwitch.show()
	%MenuFight.hide()

func _on_capture_pressed() -> void:
	if enemy.get_child(0).Health <= 15:
		enemy.get_child(0).capture()
		MySpriteData.addEXP(10)
		if "PinkMon" in enemy.get_child(0).name:
			MySpriteData.addDokiMon("PinkMon")
		elif "Purple" in enemy.get_child(0).name:
			MySpriteData.addDokiMon("PurpleMon")
		elif "Green" in enemy.get_child(0).name:
			MySpriteData.addDokiMon("GreenMon")
		%Fight.disabled = true
		%Switch.disabled = true
		%Capture.disabled = true
		%Flee.disabled = true
		new_battle_victory_defeat.visible = true
		new_battle_victory_defeat.battle_result_button.text = "捕捉成功"
		Utils.save_game()
	else:
		%Action.text = "The Enemy Dokimon's Health is not low enough!"
	

func _on_flee_pressed() -> void:
	Utils.save_game()
	%BattleLoadingAnimation.battle_loading_animation_play()
	await get_tree().create_timer(0.8).timeout
	battle_ground.queue_free()
	
func _on_fight_back_pressed() -> void:
	%MenuFight.hide()
	%MenuSwitch.hide()
	%Menu.show()

func _on_switch_back_pressed() -> void:
	%MenuFight.hide()
	%MenuSwitch.hide()
	%Menu.show()
	
func _on_attack_pressed(extra_arg_0: int) -> void:
	if MySpriteData.SelectedDokiMons[selected]["Attacks"][extra_arg_0]["Target"] == "Monster":
		var tempDic = MySpriteData.SelectedDokiMons[selected]["Attacks"]
		enemy.get_child(0).hit(tempDic[extra_arg_0]["Name"], tempDic[extra_arg_0]["Damage"])
		%Action.text = "Attacking"
		%Attack1.disabled = true
		%Attack2.disabled = true
		%Attack3.disabled = true
		battle_ground.MonsterTurn()
		await get_tree().create_timer(2).timeout
		%Attack1.disabled = false
		%Attack2.disabled = false
		%Attack3.disabled = false

func _on_dokimon_pressed(extra_arg_0: int) -> void:
	selected = extra_arg_0
	for i in player.get_child_count():
		player.get_child(i).get_node("CaptureAnimationPlayer").play_backwards("Spawn")
	var path = "../Player/" + str(MySpriteData.SelectedDokiMons[extra_arg_0]["Name"])
	get_node(path).get_node("CaptureAnimationPlayer").play("Spawn")
	get_node(path).show()
	%Dokimon1.disabled = true
	%Dokimon2.disabled = true
	%Dokimon3.disabled = true
	await get_node(path).get_node("CaptureAnimationPlayer").animation_finished
	for i in player.get_child_count():
		player.get_child(i).hide()
	get_node(path).show()
	switchUpdate()
	battle_ground.MonsterTurn()
	await get_tree().create_timer(2).timeout
	%Dokimon1.disabled = false
	%Dokimon2.disabled = false
	%Dokimon3.disabled = false
	
func switchUpdate():
	for i in 3:
		var path = "MenuSwitch/GridContainer/Dokimon" + str((i+1))
		get_node(path).hide()
	for i in MySpriteData.SelectedDokiMons:
		if i != selected and i < 3:
			var path = "MenuSwitch/GridContainer/Dokimon" + str((i+1))
			get_node(path).text = MySpriteData.SelectedDokiMons[i]["Name"]
			get_node(path).show()

